﻿using System;
using System.Windows;
using System.Windows.Threading;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.DL.Data.Common;
using ScrumTable.UI.ViewModel;
using ScrumTable.UI.ViewModel.Controller;
using ScrumTable.UI.ViewModel.Data;

namespace ScrumTable.UI.View.WinTouch.UserControls
{
	/// <summary>
	/// Interaction logic for Marker.xaml
	/// </summary>
	public partial class SaveScreen
    {

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
        private ScrumViewModelData _scrumData;
        private Action<SaveScreen, SaveState> _action;
        private ICancellableDataResult _stop;
        private Member _member;
        private DispatcherTimer _dispachTimer;

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        public SaveScreen(ScrumViewModelData scrumData, Action<SaveScreen, SaveState> action) : this(scrumData, action, null) { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="scrumData"></param>
        /// <param name="action"></param>
        /// <param name="changeToUser">if this value is not null the user will be changed</param>
        public SaveScreen(ScrumViewModelData scrumData, Action<SaveScreen, SaveState> action, Member changeToUser)
        {
            _scrumData = scrumData;
            _action = action;
            _member = changeToUser;
            DataContext = this;

            InitializeComponent();
            Visibility = Visibility.Hidden;

            _dispachTimer = new DispatcherTimer();
            _dispachTimer.Tick += (sender, args) => Visibility = Visibility.Visible;
            _dispachTimer.Interval = TimeSpan.FromMilliseconds(500);
            _dispachTimer.Start();

            if (changeToUser != null)
            {
                SaveText.Content = Localization.LanguageDictionary.Current.Translate<string>("Save_User_FaileText", "Content");
                ButtonQuit.Content = Localization.LanguageDictionary.Current.Translate<string>("Save_User_Quit",
                                                                                               "Content");
            }
            Loaded += new RoutedEventHandler(OnLoaded);
        }



        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        private void SaveCompleted(IAsyncResult result)
        {
            if (_dispachTimer.IsEnabled)
            {
                _dispachTimer.Stop(); Visibility = Visibility.Visible;
            }

            _stop = (ICancellableDataResult)result;
            WaitImage.Pause();
            if (!_stop.DataAccepted)
            {
                Menu.Visibility = Visibility.Visible;
            }
            else
            {
                _action(this, SaveState.Completed);
            }
        }

        private void Save()
        {
            if (_member == null)
            {
                _scrumData.CurrentProject.SaveData(SaveCompleted);
            }
            else
            {
                _scrumData.DataContext.ChangeUser(_member, SaveCompleted);
            }
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------
        private void OnReTry(object sender, RoutedEventArgs e)
        {
            WaitImage.Resume();
            Menu.Visibility = Visibility.Hidden;
            _stop.Cancel();
            Save();
        }

        private void OnCancel(object sender, RoutedEventArgs e)
        {
            _stop.Cancel();
            _action(this, SaveState.Cancel);
        }

        private void OnQuit(object sender, RoutedEventArgs e)
        {
            if (_member != null)
            {
                _scrumData.DataContext.ChangeUser(_member);
            }
            _action(this, SaveState.Quit);
            
        }

        void OnLoaded(object sender, RoutedEventArgs e)
        {
            if (_scrumData.CurrentProject == null)
            {
                _action(this, SaveState.Completed);
                return;
            }
            Save();
        }

        #endregion


    }
}